﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sern.Abstracts.Tracker.DataContracts;
using Sern.Common;
using Sern.Common.Fakes;
using Sern.ServiceImplementation.TrackerDefinition;
using Sern.ServiceImplementation.TrackerDefinition.Data;
using Microsoft.Practices.Unity;
using Sern.ServiceImplementation.TrackerDefinition.Service;

namespace Sern.Test.ServiceImplementation.TrackerDefinition.Service
{
    [TestClass]
    public class Tracker_ActiveClientSearch_OnlyReturnsActiveClient
    {
        [TestInitialize]
        public void Init()
        {
            ComponentLocator.Reset();

            ComponentLocator.Unity.RegisterInstance(
                RepositoryFactory.CreateStub<Client>());

            ComponentLocator.Unity.RegisterInstance(
                RepositoryFactory.CreateStub<KeyTree>());

            ComponentLocator.Unity.RegisterInstance(
                RepositoryFactory.CreateStub<KeyAtClientLog>());

            ComponentLocator.Unity.RegisterTestInstance(Guid.NewGuid(),"JobId");

            ComponentLocator.Unity.Resolve<IRepository<Client>>()
                .AddMany(new []
                    {
                        new ClientStatusInfo
                            {
                                Id = Guid.NewGuid(),
                                JobId = ComponentLocator.Unity.ResolveTest<Guid>("JobId"),
                                Uri = "http://myService1",
                                ClientHostUri = "http://myServiceHost1",
                                KeyShape = "keyShape",
                                ValueShape = "valueShape",
                                LastAlivePing = DateTime.UtcNow,
                                StartOfSubscription = DateTime.UtcNow,
                                Type = (int) ClientType.Splitter
                            },
                        new ClientStatusInfo
                            {
                                Id = Guid.NewGuid(),
                                JobId = ComponentLocator.Unity.ResolveTest<Guid>("JobId"),
                                Uri = "http://myService1",
                                ClientHostUri = "http://myServiceHost1",
                                KeyShape = "keyShape",
                                ValueShape = "valueShape",
                                LastAlivePing = TrackerCommon.LastValidActivityTime - new TimeSpan(0,0,5,0),
                                StartOfSubscription = TrackerCommon.LastValidActivityTime - new TimeSpan(0,0,5,0),
                                Type = (int) ClientType.Splitter
                            },
                        new ClientStatusInfo
                            {
                                JobId = ComponentLocator.Unity.ResolveTest<Guid>("JobId"),
                                Id = Guid.NewGuid(),
                                Uri = "http://myService1",
                                ClientHostUri = "http://myServiceHost1",
                                KeyShape = "keyShape",
                                ValueShape = "valueShape",
                                LastAlivePing = DateTime.UtcNow,
                                StartOfSubscription = DateTime.UtcNow,
                                EndOfSubscription = DateTime.UtcNow,
                                Type = (int) ClientType.Splitter
                            },
                    });
        }

        //TODO: Test with various active date.
        //TODO: Test with varoious last ping status.
        //TODO: Test with filter and non filter.
        //TODO: Test with various processing count.
        //TODO: Test with various shapes.

        [TestMethod]
        public void TestOnlyActiveClientIsReturned()
        {
            var jobId = ComponentLocator.Unity.ResolveTest<Guid>("JobId");
            var knownActiveClient = ComponentLocator.Unity.Resolve<IRepository<Client>>().Records.First();
            
            var activeClientInfo = new Tracker().FindActiveClientInfo(jobId, ClientType.Splitter,
                Guid.Empty, new Guid(),
                "keyShape",
                "valueShape"
            );

            Assert.IsNotNull(activeClientInfo);

            Assert.AreEqual(knownActiveClient.Id, activeClientInfo.Id);

            Assert.AreEqual(knownActiveClient.Type, (int) activeClientInfo.Type);

            Assert.AreEqual(knownActiveClient.ValueShape, activeClientInfo.ValueShape);
        }
    }
}
